﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBUtility;
using System.Collections;
using System.Data.OracleClient;

namespace TierApplication
{
    public partial class T_CART
    {
        public DataSet GetCartByWhere(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select a.*,b.* ");
            strSql.Append(", (select codename from c_lifetime t where b.lifetime=t.codeid) timeName");
            strSql.Append(", (select codename from c_bandwidth t where b.bandwidth=t.codeid) bandName");
            strSql.Append("  FROM T_CART a,t_product b ");
            strSql.Append(" where a.productid=b.productid ");

            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }

        public bool UpdateCartList(List<T_CART> list)
        {
            List<string> sqlList = new List<string>();
            foreach (T_CART ent in list)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update T_CART set ");
                strSql.Append("UNITPRICE=" + ent.UNITPRICE + ",");
                strSql.Append("QUANTITY=" + ent.QUANTITY + ",");
                strSql.Append("FEE=" + ent.QUANTITY * ent.UNITPRICE + " ");
                strSql.Append(" where CARTID= '" + ent.CARTID+"'");
                strSql.Append(" and PRODUCTID= '" + ent.PRODUCTID + "'");
                sqlList.Add(strSql.ToString());
            }
            return DbHelperOra.ExecuteSqlTran(sqlList) > 0;
        }

        public bool DeleteByAccountid(string ACCOUNTID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CART ");
            strSql.Append(" where ACCOUNTID=:ACCOUNTID ");
            OracleParameter[] parameters = {
					new OracleParameter("ACCOUNTID", OracleType.VarChar,50)};
            parameters[0].Value = ACCOUNTID;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int GetProductCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select count(*)  ");
            strSql.Append(" FROM T_CART ");
            strSql.Append(" where 1=1" + strWhere);
            DataSet ds=DbHelperOra.Query(strSql.ToString());
            if(ds!=null && ds.Tables.Count!=0 && ds.Tables[0].Rows.Count!=0)
            {
                return Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            }
            else
            {
                return 0;
            }
        }

        public bool UpdateCartCount(T_CART ent)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CART set ");
            strSql.Append("QUANTITY=" + ent.QUANTITY + " ");
            strSql.Append(" where CARTID= '" + ent.CARTID + "'");
            return DbHelperOra.ExecuteSql(strSql.ToString()) > 0;
        }

        /// <summary>
        /// 取得购物车ID
        /// </summary>
        /// <returns></returns>
        public string GetCartId()
        {
            string sql = "Select get_cartid() From dual";
            DataSet ds = DbHelperOra.ExecuteDataSet(sql);
            if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0)
            {
                return ds.Tables[0].Rows[0][0].ToString();
            }
            else
            {
                return "-1";
            }
        }
    }
}
